package com.pzb.web.controller.goods;

import java.util.List;

import com.pzb.common.core.domain.entity.GdGoodsClass;
import com.pzb.wechat.service.IWechatAppletService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pzb.common.annotation.Log;
import com.pzb.common.core.controller.BaseController;
import com.pzb.common.core.domain.AjaxResult;
import com.pzb.common.enums.BusinessType;
import com.pzb.goods.service.IGdGoodsClassService;


/**
 * 商品分类Controller
 * 
 * @author pzb
 * @date 2021-09-22
 */
@RestController
@RequestMapping("/goods/goodsClass")
public class GdGoodsClassController extends BaseController
{
    @Autowired
    private IGdGoodsClassService gdGoodsClassService;
    @Autowired
    private IWechatAppletService wechatAppletService;

    /**
     * 查询商品分类列表
     */
    @PreAuthorize("@ss.hasPermi('goods:goodsClass:list')")
    @GetMapping("/list")
    public AjaxResult list(GdGoodsClass gdGoodsClass)
    {
        gdGoodsClass.setExtAppid(wechatAppletService.getExtAppid());
        List<GdGoodsClass> list = gdGoodsClassService.selectGdGoodsClassList(gdGoodsClass);
        return AjaxResult.success(list);
    }

    /**
     * 获取商品分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('goods:goodsClass:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(gdGoodsClassService.selectGdGoodsClassById(id));
    }

    /**
     * 新增商品分类
     */
    @PreAuthorize("@ss.hasPermi('goods:goodsClass:add')")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GdGoodsClass gdGoodsClass)
    {
        String extAppid = wechatAppletService.getExtAppid();
        if("1".equals(extAppid)){
            return AjaxResult.error("您还没有小程序!");
        }
        gdGoodsClass.setExtAppid(extAppid);
        return toAjax(gdGoodsClassService.insertGdGoodsClass(gdGoodsClass));
    }

    /**
     * 修改商品分类
     */
    @PreAuthorize("@ss.hasPermi('goods:goodsClass:edit')")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GdGoodsClass gdGoodsClass)
    {
        return toAjax(gdGoodsClassService.updateGdGoodsClass(gdGoodsClass));
    }

    /**
     * 修改商品分类
     */
    @PreAuthorize("@ss.hasPermi('goods:goodsClass:edit')")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PutMapping("changeStatus")
    public AjaxResult changeStatus(@RequestBody GdGoodsClass gdGoodsClass)
    {
        //修改下级分类状态
        gdGoodsClassService.changeClassStatusByParentId(gdGoodsClass);
        return toAjax(gdGoodsClassService.updateGdGoodsClass(gdGoodsClass));
    }

    /**
     * 删除商品分类
     */
    @PreAuthorize("@ss.hasPermi('goods:goodsClass:remove')")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        if (gdGoodsClassService.hasChildByClassId(id))
        {
            return AjaxResult.error("存在下级分类,不允许删除");
        }
        if (gdGoodsClassService.checkClassExistGoods(id))
        {
            return AjaxResult.error("分类存在商品,不允许删除");
        }
        return toAjax(gdGoodsClassService.deleteGdGoodsClassById(id));
    }
}
